package hik;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import com.sun.jna.Pointer;

/**
 * 红外长连接 获取实时温度 回调函数
 * 
 * @author fankai 2019-12-27 09:35:39
 *
 */
public class RealtimeCallback implements HCNetSDK.FRemoteConfigCallback {
	
	HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD thermometry = new HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD();
	
	String ruleName ="";
	StringBuffer ana_str = new StringBuffer();
	float fMaxTemp = 0;
	float fMinTemp = 0;
	float fAvgTemp = 0;
	
	float max_fX = 0;
	float max_fY = 0;
	float min_fX = 0;
	float min_fY = 0;
	
	public static int byteArrayToInt(byte[] bytes) {  
	    int value = 0;  
	    // 由高位到低位  
	    for (int i = 0; i < 4; i++) {  
	        int shift = (4 - 1 - i) * 8;  
	        value += (bytes[i] & 0x000000FF) << shift;// 往高位游  
	    }  
	    return value;  
	}  
	
	/**
	 *  回调函数
	 * @date  2020/2/20 22:23
	 * @author fankai
	 * @param dwType, pBuffer, dwBufLen, dwUser]
	 **/
	@Override
	public void invoke(int dwType, Pointer pBuffer, int dwBufLen, Pointer dwUser) {
		System.out.println("--------------"+dwType+"------------------");
		switch (dwType) {
		case HCNetSDK.NET_SDK_CALLBACK_TYPE_STATUS:// 回调状态值
			byte[] byteArray = pBuffer.getByteArray(0, pBuffer.SIZE);
			System.out.println(byteArray);
			int byteArrayToInt = byteArrayToInt(byteArray);
			System.out.println(byteArrayToInt);
			int int1 = pBuffer.getInt(0);
			switch (int1) {
			case HCNetSDK.NET_SDK_CALLBACK_STATUS_SUCCESS: // 成功;
				System.out.println("回调成功：dwType: " + dwType );
				break;
			case HCNetSDK.NET_SDK_CALLBACK_STATUS_PROCESSING: // 处理中
				System.out.println("回调处理中：dwType: " + dwType );
				break;
			case HCNetSDK.NET_SDK_CALLBACK_STATUS_FAILED: // 失败
				System.out.println("回调失败：dwType: " + dwType +",status: "+int1+",error: "+pBuffer.getFloat(0));
				break;
			default:
				break;
			}
			break;
		case HCNetSDK.NET_SDK_CALLBACK_TYPE_PROGRESS: // 回调进度值
			System.out.println("回调进度值,错误码:" + pBuffer.getInt(4));
			break;
		case HCNetSDK.NET_SDK_CALLBACK_TYPE_DATA: // 回调数据内容
			
			HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD thermometry = new HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD();
			String ruleName ="";
			StringBuffer ana_str = new StringBuffer();
			float fMaxTemp = 0;
			float fMinTemp = 0;
			float fAvgTemp = 0;
			
			float max_fX = 0;
			float max_fY = 0;
			float min_fX = 0;
			float min_fY = 0;
			
			Map<String,Object> map = new HashMap<>();
			Map<Integer, Map<String, String>> anasMap = new HashMap<>();
			Map<String, String> aMap = new HashMap<>();
			String presetid = "";
			String anaStr = "";
			int presetno = 0;
			
			// 将 Pointer 转化为 java类
			thermometry = new HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD();
			thermometry.write();
			Pointer pthermometry = thermometry.getPointer();
			pthermometry.write(0, pBuffer.getByteArray(0, thermometry.size()), 0, thermometry.size());
			thermometry.read();
			ruleName = new String(thermometry.szRuleName).trim();

			ana_str = new StringBuffer();
			fMaxTemp = 0;
			fMinTemp = 0;
			fAvgTemp = 0;
			
			max_fX = 0;
			max_fY = 0;
			min_fX = 0;
			min_fY = 0;
			if (thermometry.byRuleCalibType == 0) {// 点测温
				fMaxTemp = fMinTemp = fAvgTemp = thermometry.struPointThermCfg.fTemperature;// 温度
			} else if (thermometry.byRuleCalibType == 1) {// 框测温
				 fMaxTemp = thermometry.struLinePolygonThermCfg.fMaxTemperature;
				 fMinTemp = thermometry.struLinePolygonThermCfg.fMinTemperature;
				 fAvgTemp = thermometry.struLinePolygonThermCfg.fAverageTemperature;

				//最高温坐标
				max_fX = thermometry.struHighestPoint.fX;
				max_fY = thermometry.struHighestPoint.fY;
				//最低温
				min_fX = thermometry.struLowestPoint.fX;
				min_fY = thermometry.struLowestPoint.fY;
			} else if (thermometry.byRuleCalibType == 2) {// 线测温
				 fMaxTemp = thermometry.struLinePolygonThermCfg.fMaxTemperature;
				 fMinTemp = thermometry.struLinePolygonThermCfg.fMinTemperature;
				 fAvgTemp = thermometry.struLinePolygonThermCfg.fAverageTemperature;

				//最高温坐标
				max_fX = thermometry.struHighestPoint.fX;
				max_fY = thermometry.struHighestPoint.fY;
				//最低温
				min_fX = thermometry.struLowestPoint.fX;
				min_fY = thermometry.struLowestPoint.fY;
				
			} else {
				return;
			}
			
			map.put("anaAlias", ruleName);
			map.put("max", fMaxTemp);
			map.put("max_x", max_fX*640);
			map.put("max_y", max_fY*480);
			map.put("min", fMinTemp);
			map.put("min_x", min_fX*640);
			map.put("min_y", min_fY*480);
			map.put("avg", fAvgTemp);
			
			System.out.println(map);
//			Jedis jedis = RedisClient.jedisPool.getResource();
//			jedis.set("aa_"+Math.random(), Math.random()+"");
//			RedisClient.close(jedis);
			
			break;
			default:
				break;
		}
	}
	
	

}
